<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/3/23
 * Time: 16:13
 * Desc: 首页服务层
 */
namespace app\index\service;
use think\Controller;
use think\Db;

class IndexService extends Controller{
    /**
     * @Author  lingyun
     * @Desc    获取顶部导航栏推荐商品 -- 限制5条
     * 2018-03-29
     */
    public function get_daohang_tui_goods(){
        //查询仅允许线上销售的商品
        $sql = "SELECT id,name,logo,cat_id FROM ys_goods AS a WHERE 6> (SELECT COUNT(*) FROM ys_goods WHERE cat_id=a.cat_id AND id>a.id) AND a.is_online=1 AND a.status=1 AND a.is_deleted=0 AND is_top>0 ORDER BY a.id DESC";

//        $sql = "SELECT id,name,logo,cat_id FROM ys_goods AS a WHERE 5> (SELECT COUNT(*) FROM ys_goods WHERE cat_id=a.cat_id AND id>a.id) AND a.is_online=1 AND a.status=1 AND a.is_deleted=0 ORDER BY a.id DESC";

        $list = Db::query($sql);
        return $list;
    }

    /**
     * @Author  lingyun
     * @Desc    获取顶部轮播图
     * 2018-03-29
     */
    public function get_top_banner(){
        $where_banner = [
            'a.status'=>1,
            'a.is_deleted'=>0,
//            'b.status'=>1,
//            'b.is_deleted'=>0
        ];
        $banner = db('banner')->alias('a')->join('ys_goods b','a.goods_id=b.id','LEFT')->where($where_banner)->order('a.dis_order desc')->field('a.*,b.name,b.price,b.unit,a.pc_logo as pc_banner,a.m_logo as mo_banner,b.status as goods_status,b.is_deleted as goods_deleted')->select();
        return $banner;
    }

    /**
     * @Author  lingyun
     * @Desc    首页广告位一
     * 2018-03-29
     */
    public function get_advert_one(){
        $where_advert_1 = [
            'a.adp'=>1,
//            'b.status'=>1,
//            'b.is_deleted'=>0
        ];
        $advert = db('advert')->alias('a')->join('ys_goods b','a.goods_id=b.id','LEFT')->where($where_advert_1)->order('a.id desc')->field('a.*,b.name,b.unit,b.price,a.pc_logo as hot_pc_banner,a.m_logo as hot_mo_banner,b.subtitle,b.base_spec,b.status as goods_status,b.is_deleted as goods_deleted')->limit(1)->find();

        return $advert;
    }

    /**
     * @Author  lingyun
     * @Desc    获取广告位二
     */
    public function get_advert_two(){
        $where_advert_2 = [
            'a.adp'=>2,
//            'b.status'=>1,
//            'b.is_deleted'=>0
        ];
        $advert = db('advert')->alias('a')->join('ys_goods b','a.goods_id=b.id','LEFT')->where($where_advert_2)->order('a.id desc')->field('a.*,b.name,b.price,b.unit,a.pc_logo,a.m_logo,b.hot_pic,b.subtitle,b.base_spec,b.status as goods_status,b.is_deleted as goods_deleted')->limit(1)->find();

        return $advert;
    }

    /**
     * @Author  lingyun
     * @Desc    获取门店优惠券
     * 2018-03-29
     */
    public function get_store_coupon(){
        $store_id = cookie('store_id');
        //门店优惠券
        $where_coupon['a.status'] = array('eq',1);
//        $where_coupon['a.is_recommend'] = array('eq',1);
        $where_coupon['a.is_deleted'] = array('eq',0);
        $where_coupon['a.type'] = array('eq',3);
        $count_coupon = 0;
        if(!empty($store_id) && !empty(session('_user_uid_.uid'))){
            $where_coupon['a.store_id'] = array('eq',$store_id);
            $coupon = db('store_coupon')->alias('a')->where($where_coupon)->field('a.reduce,a.store_id,a.id,(select count(*) from ys_user_ycard where uid='.session("_user_uid_.uid").' and coupon_id=a.id) as is_receive')->order('a.id desc')->limit(2)->select();

            if(!empty($coupon)){
                foreach($coupon as $k => $v){
                    if($v['is_receive'] == 0){
                        $count_coupon++;
                    }
                }
            }

        }else{
            $coupon = array();
        }

        $this->assign('count_coupon',$count_coupon);
        return $coupon;
    }

    /**
     * @Author  lingyun
     * @Desc    获取门店推荐商品分类
     * 2018-03-29
     */
    public function get_recommend_category(){
        //推荐商品分类
        $where_category_1 = [
            'is_recommend'=>1
        ];
        $where_category_2 = [
            'is_recommend'=>2
        ];
        $recommend_category_1 = db('goods_category')->where($where_category_1)->find();
        $recommend_category_2 = db('goods_category')->where($where_category_2)->find();

        return ['r_c_one'=>$recommend_category_1,'r_c_two'=>$recommend_category_2];
    }

    /**
     * @Author  lingyun
     * @Desc    获取推荐商品分下的商品
     * 2018-03-29
     */
    public function get_recommend_category_goods(){
        $category = $this->get_recommend_category();            //推荐商品的分类
        //推荐的商品
        $where_recommend_goods_1 = [
            'a.is_deleted'=>0,
            'a.status'=>1,
            'a.cate_id'=>$category['r_c_one']['id'],
//            'b.status'=>1,
//            'b.is_deleted'=>0
        ];
        $where_recommend_goods_2 = [
            'a.is_deleted'=>0,
            'a.status'=>1,
            'a.cate_id'=>$category['r_c_two']['id'],
//            'b.status'=>1,
//            'b.is_deleted'=>0
        ];
        //推荐商品一位
        $recommend_goods_1 = db('goods_recommend')->alias('a')->join('ys_goods b','a.goods_id=b.id','LEFT')->where($where_recommend_goods_1)->field('a.*,b.name,b.unit,b.price,b.subtitle,b.logo,b.status as goods_status,b.is_deleted as goods_deleted')->limit(3)->select();

        //推荐商品二位
        $recommend_goods_2 = db('goods_recommend')->alias('a')->join('ys_goods b','a.goods_id=b.id','LEFT')->where($where_recommend_goods_2)->field('a.*,b.name,b.subtitle,b.price,b.unit,b.logo,b.status as goods_status,b.is_deleted as goods_deleted')->limit(4)->select();

        return ['r_g_one'=>$recommend_goods_1,'r_g_two'=>$recommend_goods_2];
    }

    /**
     * @Author  lingyun
     * @Desc    商品标签
     * 2018-03-29
     */
    public function get_goods_label(){
        $goods_label = db('goods_label')->where(['status'=>1,'is_deleted'=>0])->select();
        return $goods_label;
    }

    /**
     * @Author  lingyun
     * @Desc    判断是否有权益优惠券
     * @param   type  1:查询权益优惠券  2：返回权益优惠券信息
     * 2018-03-23
     */
    public function qy_coupon($type){
        $uid = session('_user_uid_.uid');
        $user_level = db('user')->where('uid',$uid)->value('level_id');         //会员当前的等级
        $store_id = cookie('store_id');
//        $month_time = get_month_time();             //获取本月的起止时间戳
        $map = [
            'a.store_id'=>['eq',$store_id],
            'a.status'=>['eq',1],
//            'a.member_level_id'=>['egt',$user_level],
//            'a.addtime'=>[['egt',$month_time['start']],['elt',$month_time['end']],'AND'],
            'b.id'=>NULL,
            'a.type'=>['eq',$type]
        ];
//        halt($map);
        //查询本月没有领取的会员优惠券
        $coupon = db('store_coupon')->alias('a')->join('ys_user_ycard b','a.id=b.coupon_id','LEFT')->where($map)->field('a.*')->select();
        $num = count($coupon);

        if($num>0){
            $this->assign('has_qy_coupon',1);
            $this->assign('qy_coupon',$coupon[0]);
        }else{
            $this->assign('has_qy_coupon',0);
            $this->assign('qy_coupon',['pc_beijing'=>'','m_beijing'=>'']);
        }



        if($type == 1){     //返回查询权益优惠券结果
            return $coupon;
        }
    }

    /**
     * @Author  lingyun
     * @Desc    领取权益优惠券
     * 2018-03-23
     */
    public function get_qy_coupon(){
        $coupon = $this->qy_coupon(1);
        $uid = session("_user_uid_.uid");
        $data = [];                 //组合入库数据
        foreach($coupon as $k=>$v){     //组合入库数据
            $data[$k]['store_id'] = $v['store_id'];
            $data[$k]['coupon_id'] = $v['id'];
            $data[$k]['uid'] = $uid;
            $data[$k]['type'] = 3;
            $data[$k]['cat_id'] = $v['cat_id'];
            $data[$k]['condition'] = $v['condition'];
            $data[$k]['morethan'] = $v['morethan'];
            $data[$k]['reduce'] = $v['reduce'];
            $data[$k]['use_start_time'] = $v['use_start_time'];
            $data[$k]['use_end_time'] = $v['use_end_time'];
            $data[$k]['addtime'] = time();
            $data[$k]['price'] = $v['price'];
            $data[$k]['value'] = $v['value'];
            $data[$k]['is_used'] = 0;
            $data[$k]['status'] = 1;
        }

        $res = db('user_ycard')->insertAll($data);      //批量新增

        return $res;
    }

    /**
     * @Author  lingyun
     * @Desc    获取购物车商品数量
     * 2018-04-08
     */
    public function get_cart_amount(){
        $store_id = cookie('store_id');         //当前门店id
        $cart = (array)cookie('user_cart');            //会员购物车
        if(!empty($store_id)){                  //
            foreach($cart as $k => $v){
                if($v['store_id'] != $store_id){
                    unset($cart[$k]);
                }
            }
            $store_goods_stock_id = array_column($cart, 'store_goods_stock_id');
            $goods = db('store_goods_stock')->alias('a')->join('ys_goods b','a.goods_id=b.id')->join('ys_goods_spec_detail c','a.goods_spec_id=c.id','LEFT')->where(['a.is_deleted'=>0,'a.onsale'=>1,'c.is_deleted'=>0,'b.is_deleted'=>0,'status'=>1,'a.id'=>['in',$store_goods_stock_id]])->select();
            return count($goods);
        }else{
            return 0;
        }
    }

    /**
     * @Author  lingyun
     * @Desc    获取会员信息
     * 2018-04-12
     */
    public function get_member_info(){
        $member = db('user')->alias('a')->join('ys_sys_level b','a.level_id=b.id','LEFT')->field('a.realname,a.mobile,b.title')->where('a.uid',session('_user_uid_.uid'))->find();

        return $member;
    }


}